package com.ruoyi.web.controller.orderManage;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.CheckinMessage;
import com.ruoyi.system.domain.OrderManage;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.Unsubscribe;
import com.ruoyi.system.service.CheckinMessageService;
import com.ruoyi.system.service.OrderManageService;
import com.ruoyi.system.service.UnsubscribeService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


@RestController
@RequestMapping("/system/orderManage")
public class OrderManageController extends BaseController {

    @Autowired
    private OrderManageService orderManageService;
    @Autowired
    private CheckinMessageService checkinMessageService;
    @Autowired
    private UnsubscribeService unsubscribeService;


    @PreAuthorize("@ss.hasPermi('system:orderManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderManage orderManage)
    {
        startPage();
        List<OrderManage> list = orderManageService.selectOrderManageList(orderManage);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:orderManage:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Map<String ,Object> map)
    {
        OrderManage orderManage = new OrderManage();
        // 15位数字的最小值是100000000000000L
        long min = 100000000000000L;
        // 15位数字的最大值是999999999999999L
        long max = 999999999999999L;

        Random random = new Random();
        // 生成15位随机数
        long random15Digits = min + ((long)(random.nextDouble() * (max - min + 1)));
        orderManage.setNumberId(random15Digits+"");
        Instant instant = Instant.parse((String) map.get("dateIn"));
        Date date = Date.from(instant);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, Integer.parseInt((String) map.get("inDays"))); // 增加1天
        Date nextDay = calendar.getTime();
        orderManage.setDateIn(date);
        orderManage.setDateOut(nextDay);

        orderManage.setRoomId((String) map.get("roomId"));
        orderManage.setReserveName((String) map.get("reserveName"));
        orderManage.setReserveDeposit((Integer) map.get("reserveDeposit"));
        orderManage.setReservePhone((String) map.get("reservePhone"));
        orderManage.setReserveType((String) map.get("reserveType"));
        orderManage.setInDays((String) map.get("inDays"));
        orderManage.setHouseType((String) map.get("houseType"));
        orderManage.setReservePrice((String) map.get("reservePrice"));
        return toAjax(orderManageService.insertOrderManage(orderManage));


    }

    @PreAuthorize("@ss.hasPermi('system:orderManage:update')")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Map<String,Object> map)
    {
        OrderManage orderManage = new OrderManage();

        OffsetDateTime odt = OffsetDateTime.parse((String) map.get("dateIn"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = odt.format(formatter);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(formattedDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, Integer.parseInt((String) map.get("inDays"))); // 增加1天
        Date nextDay = calendar.getTime();
        orderManage.setDateIn(date);
        orderManage.setDateOut(nextDay);

        orderManage.setNumberId((String) map.get("numberId"));
        orderManage.setRoomId((String) map.get("roomId"));
        orderManage.setReserveName((String) map.get("reserveName"));
        orderManage.setReserveDeposit((Integer) map.get("reserveDeposit"));
        orderManage.setReservePhone((String) map.get("reservePhone"));
        orderManage.setReserveType((String) map.get("reserveType"));
        orderManage.setInDays((String) map.get("inDays"));
        orderManage.setHouseType((String) map.get("houseType"));
        orderManage.setReservePrice((String) map.get("reservePrice"));
        return toAjax(orderManageService.updateOrderManage(orderManage));
    }

    @PreAuthorize("@ss.hasPermi('system:orderManage:delete')")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody String numberId)
    {
        return toAjax(orderManageService.deleteOrderManage(numberId));
    }

    @PreAuthorize("@ss.hasPermi('system:orderManage:checkIn')")
    @PostMapping("/checkIn")
    public AjaxResult checkIn(@RequestBody String numberId)
    {
        OrderManage orderManage = new OrderManage();
        orderManage.setNumberId(numberId);
        List<OrderManage> list = orderManageService.selectOrderManageList(orderManage);
        CheckinMessage checkinMessage = new CheckinMessage();
        checkinMessage.setNumberId(list.get(0).getNumberId());
        checkinMessage.setRoomId(list.get(0).getRoomId());
        checkinMessage.setCheckinName(list.get(0).getReserveName());
        checkinMessage.setCheckinDeposit(100);
        checkinMessage.setCheckinRoom(list.get(0).getHouseType());
        checkinMessage.setCheckinType(list.get(0).getReservePrice());
        checkinMessage.setCheckinPrice((Integer)list.get(0).getReserveDeposit());
        checkinMessage.setCheckinTime(list.get(0).getDateIn());
        checkinMessage.setCheckinDate(list.get(0).getDateOut());
        checkinMessageService.insertCheckinMessage(checkinMessage);
        return toAjax(orderManageService.deleteOrderManage(orderManage.getNumberId()));
    }

    @PreAuthorize("@ss.hasPermi('system:orderManage:checkOut')")
    @PostMapping("/checkOut")
    public AjaxResult checkOut(@RequestBody Map<String,Object> map)
    {
        OrderManage orderManage = new OrderManage();
        orderManage.setNumberId(map.get("numberId").toString());
        List<OrderManage> list = orderManageService.selectOrderManageList(orderManage);
        Unsubscribe unsubscribe = new Unsubscribe();
        unsubscribe.setNumberId(list.get(0).getNumberId());
        unsubscribe.setRoomId(list.get(0).getRoomId());
        unsubscribe.setCancelName(list.get(0).getReserveName());
        unsubscribe.setCancelReason(map.get("reserveReason").toString());
        unsubscribe.setCancelCheckOut("退订");
        unsubscribe.setCancelCost((Integer) map.get("reserveDeposit"));
        unsubscribe.setCancelType(list.get(0).getReserveType());
        unsubscribe.setDateOut(new Date());
        unsubscribeService.insertUnsubscribe(unsubscribe);
        return toAjax(orderManageService.deleteOrderManage(orderManage.getNumberId()));
    }

}
